﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using WFL.IoCUnity;

namespace WFL.RBAC
{
    public class RoleBLL
    {
        private IRole _role;

        public RoleBLL()
        {
            _role = UnityUtility.Resolve<IRole>();
        }

        public void Create(Role item)
        {
            _role.Create(item);
        }

        public void Delete(int id)
        {
            _role.Delete(id);
        }

        public void Update(Role item)
        {
            _role.Update(item);
        }

        public IList<Role> GetList(string name, int pageIndex, int pageSize)
        {
            return _role.GetList(name, pageIndex, pageSize);
        }

        public Role Get(int id)
        {
            return _role.Get(id);
        }

        public void BindPermission(Permission permission)
        {
            _role.BindPermission(permission);
        }

        public void UpdatePermission(Permission permission)
        {
            _role.UpdatePermission(permission);
        }

        public void UnbindPermission(int id, int resourceId)
        {
            _role.UnbindPermission(id, resourceId);
        }

        public void UnbindPermission(int id)
        {
            _role.UnbindPermission(id);
        }

        public IList<Permission> GetPermissionList(int id)
        {
            Role item = _role.Get(id);
            IList<Permission> permissions = _role.GetPermissionList(id);
            IList<Role> combinedRoles = _role.GetCombinedRoleList(id);
            if (combinedRoles == null || combinedRoles.Count == 0)
            {
                return permissions;
            }
            foreach (Role role in combinedRoles)
            {
                IList<Permission> combinedPermissions = _role.GetPermissionList(role.Id);
                if (combinedPermissions == null || combinedPermissions.Count == 0)
                {
                    continue;
                }
                foreach (Permission permission in permissions)
                {
                    Permission targetPermission = null;
                    foreach (Permission combinedPermission in combinedPermissions)
                    {
                        if (combinedPermission.ResourseId == permission.ResourseId)
                        {
                            targetPermission = combinedPermission;
                            break;
                        }
                    }
                    if (targetPermission == null)
                    {
                        combinedPermissions.Add(permission);
                    }
                    else
                    {
                        targetPermission.Union(permission);
                    }
                }
                permissions = combinedPermissions;
            }
            return permissions;
        }
    }
}
